package cn.wizzer.app.crown.modules.services.impl;

import cn.wizzer.app.crown.modules.models.CrownViewPoint;
import cn.wizzer.app.crown.modules.models.CrownViewPointLabel;
import cn.wizzer.app.crown.modules.services.ViewPointLabelService;
import cn.wizzer.app.crown.modules.services.ViewPointService;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.math.BigDecimal;
import java.util.List;

@IocBean(args = "refer:dao")
@Service(interfaceClass = ViewPointLabelService.class)
public class ViewPointLabelServiceImpl extends BaseServiceImpl<CrownViewPointLabel> implements ViewPointLabelService {
    public ViewPointLabelServiceImpl(Dao dao) {
        super(dao);
    }
    @Inject
    private ViewPointService viewPointService;


    List<Integer> arr = Result.createList(3,5,7,9,10,20,30);
    @Override
    public boolean statistics(CrownViewPoint viewPoint) {
        if (viewPoint.getResult()==null||viewPoint.getResult().equals(0)) {
            return false;
        }
        Cnd finishedViewPointQuery = Cnd.where("userId", "=", viewPoint.getUserId()).andNot("result", "=", 0).and("matchType","=", viewPoint.getMatchType());
        //移除无效标签
        this.vDelete(Cnd.where("userId", "=", viewPoint.getUserId()).and("delFlag","=",0));
        //该用户下的所有已结算观点
        List<CrownViewPoint> viewPoints = viewPointService.query(finishedViewPointQuery.orderBy("opAt", "desc"));
        //近场统计
        for (Integer len : arr) {
            if (viewPoints.size()>=len) {
                List<CrownViewPoint> crownViewPoints = viewPoints.subList(0, len);
                BigDecimal totalOdds = BigDecimal.ZERO;
                for (CrownViewPoint crownViewPoint : crownViewPoints) {
                    if(crownViewPoint.getResult() == 1){
                        totalOdds = crownViewPoint.getOdds();
                    }
                }
                CrownViewPointLabel label = new CrownViewPointLabel();
                label.setMatchCount(len);
                label.setTotalOdds(totalOdds);
                label.setWinCount(Math.toIntExact(crownViewPoints.stream().filter(vp -> vp.getResult().equals(1)).count()));
                label.setUserId(viewPoint.getUserId());
                this.insert(label);
            }
        }

        //update总胜率 总回报率
        CrownViewPointLabel label = new CrownViewPointLabel();
        BigDecimal allTotalOdds = BigDecimal.ZERO;
        for (CrownViewPoint point : viewPoints) {
            if(point.getResult() == 1){
                allTotalOdds = point.getOdds();
            }
        }
        label.setMatchCount(viewPoints.size());
        label.setTotalOdds(allTotalOdds);
        label.setWinCount(Math.toIntExact(viewPoints.stream().filter(vp -> vp.getResult().equals(1)).count()));
        label.setUserId(viewPoint.getUserId());
        label.setType("all");
        this.insert(label);
        //总连胜
        int winCount = 0;
        winCount:
        for (int i = viewPoints.size() - 1; i >= 0; i--) {
            if (viewPoints.get(i).getResult().equals(1)){
                winCount = i+1;
            }else break winCount;
        }
        if (winCount>1){
            label = new CrownViewPointLabel();
            label.setType("seriesWin");
            label.setMatchCount(winCount);
            label.setWinCount(winCount);
            label.setUserId(viewPoint.getUserId());
            label.setTotalOdds(viewPoints.subList(0,winCount-1).stream().map(CrownViewPoint::getOdds).reduce(BigDecimal.ZERO,BigDecimal::add));
            this.insert(label);
        }
        //总连跪
        int transportCount = 0;
        transportCount:
        for (int i = viewPoints.size() - 1; i >= 0; i--) {
            if (viewPoints.get(i).getResult().equals(-1)){
                transportCount = i+1;
            }else break transportCount;
        }
        if (transportCount>1){
            label = new CrownViewPointLabel();
            label.setType("seriesTransport");
            label.setMatchCount(transportCount);
            label.setWinCount(transportCount);
            label.setUserId(viewPoint.getUserId());
            label.setTotalOdds(viewPoints.subList(0,transportCount-1).stream().map(CrownViewPoint::getOdds).reduce(BigDecimal.ZERO,BigDecimal::add));
            this.insert(label);
        }

        return true;
    }
}
